import { rmdir_recursive, block_sleep } from './utils.js';
const { execSync } = window.require('child_process');
const fs = window.require('fs');

function generate_cmd_prefix(tool_path, dev_id) {
    if (tool_path.length === 0 || dev_id.length === 0)
        return '';

    return tool_path + ' -s ' + dev_id;
}

let device_list_path = '/sdcard/Android/data/com.huawei.gameperftool/files/';

function wait_app_list_finish(cmd_prefix) {
    let cmd = cmd_prefix + ' shell \"ls -al ' + device_list_path + 'finish\"';
    let wait_time = 0;
    let check_interval = 100;
    let wait_timeout = 5000;

    while (wait_time < wait_timeout) {
        try {
            execSync(cmd);
            break;
        } catch (err) {
            block_sleep(check_interval);
            wait_time += check_interval;
        }
    }

    if (wait_time >= wait_timeout)
        return -1;

    return 0;
}

function convert_app_list(cmd_prefix) {
    let list = [];
    let app_list_tmp_path = './src/pages/game/app_list';

    try {
        fs.accessSync(app_list_tmp_path, fs.constants.R_OK);
        if (rmdir_recursive(app_list_tmp_path) !== 0)
            return [];
    } catch (err) {}

    let cmd = cmd_prefix + ' pull ' + device_list_path + ' ' + app_list_tmp_path;
    try {
        execSync(cmd);
    } catch (err) {
        console.log(err);
        return [];
    }

    const list_file_info = fs.readFileSync(app_list_tmp_path + '/app_list.txt', 'utf-8');
    let lines = list_file_info.split(/[(\r\n)\r\n]/);
    let is_app_name = true;
    let current_app_name;
    let current_app_icon;

    for (let line of lines) {
        if (line.length === 0)
            break;

        if (is_app_name) {
            let app_icon = '';
            try {
                app_icon = fs.readFileSync(app_list_tmp_path + '/' + line + '.png').toString('base64');
            } catch (err) {

            }
            current_app_name = line;
            current_app_icon = app_icon;
            is_app_name = false;
        } else {
            is_app_name = true;
            let app_show_info = { name: current_app_name, icon: current_app_icon, package: line };
            list.push(app_show_info);
        }
    }
    return list;
}

export function android_get_app_list(tool_path, dev_id) {
    let cmd_prefix = generate_cmd_prefix(tool_path, dev_id);

    if (cmd_prefix.length === 0) {
        return [];
    }

    let cmd = cmd_prefix + ' shell \"ls -al /sdcard/Android\"';
    try {
        execSync(cmd); //used to start adb server
    } catch (err) {}

    cmd = cmd_prefix + ' shell \"ls -al ' + device_list_path + '\"';
    try {
        execSync(cmd);
        cmd = cmd_prefix + ' shell \"rm -rf ' + device_list_path + '/*\"';
        execSync(cmd);
    } catch (err) {}

    cmd = cmd_prefix + " shell \"am start-foreground-service -n com.huawei.gameperftool/.GetAppListService\"";
    try {
        execSync(cmd);
    } catch (err) {
        return [];
    }

    if (wait_app_list_finish(cmd_prefix) < 0) {
        console.log('Wait app list finish timeout');
        return [];
    }

    cmd = cmd_prefix + " shell \"am stop-service -n com.huawei.gameperftool/.GetAppListService\"";
    try {
        execSync(cmd);
    } catch (err) {}

    return convert_app_list(cmd_prefix)
}